package com.anthonybennis.blender.client;

import com.anthonybennis.blender.client.Navigatgor.PAGE;
import com.anthonybennis.blender.client.images.ImageClientBundle;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.MGWTSettings;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BlenderShortCuts implements EntryPoint 
{
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() 
	{
		 // set viewport and other settings for mobile
	    MGWT.applySettings(MGWTSettings.getAppSetting());
	    ImageClientBundle imageClientBunde = (ImageClientBundle)GWT.create(ImageClientBundle.class);
	    
		DockLayoutPanel mainPanel = new DockLayoutPanel(Unit.EM);
		mainPanel.setStyleName("main-panel");
		
		/*
		 * Header
		 */
		Navigatgor navigator = new Navigatgor(mainPanel, imageClientBunde);
		MGWTHeaderPanel headerPanel = new MGWTHeaderPanel(imageClientBunde, navigator);
		headerPanel.createBlenderCompanionHeader();
		mainPanel.addNorth(headerPanel, 3);
		
		/*
		 * Footer
		 */
		LayoutPanel footerPanel = new LayoutPanel();
		footerPanel.setStyleName("footer-panel");
		Label copyrightLabel = new Label("\u00A9 Anthony Bennis 2015");
		copyrightLabel.setStyleName("footer-label");
		footerPanel.add(copyrightLabel);
		mainPanel.addSouth(footerPanel, 2);		
		
		/*
		 * 
		 */
		navigator.open(PAGE.TIP_OF_THE_DAY);
		
		/*
		 * Root
		 */
		RootPanel.get().setStyleName("root-panel");
		RootLayoutPanel.get().add(mainPanel);
		
		this.preventUserHighlightingComponents(mainPanel);
	}
	
	/**
	 * 
	 * @param widget
	 */
	private void preventUserHighlightingComponents(final Widget widget)
	{
		Event.addNativePreviewHandler(new NativePreviewHandler() {
            @Override
            public void onPreviewNativeEvent(NativePreviewEvent event) {
                EventTarget target = event.getNativeEvent().getEventTarget();
                if (!target.equals(widget.getElement())){
                    event.getNativeEvent().preventDefault();
                }
            }
        });
	}
}